package a;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

//p186 任务实施
class Prize<T>{
    private T prize;
    private ArrayList<T> prizes;

    public T getPrize() {//抽奖方法
        Random random = new Random();//创建一个随机数
        return prizes.get(random.nextInt(prizes.size()));//随机产生奖品
    }

    public void setPrize(ArrayList<T> prizes) {//设置奖品
        this.prizes = prizes;
    }
}
public class demo4 {
    public static void main(String[] args) {
        Prize<String> prize1 = new Prize<String>();//创建泛型类对象，类型为字符串
        ArrayList<String> prod1  = new ArrayList<String>();//创建奖品池对象
        //向奖品池中添加奖品
        prod1.add("电饭煲");
        prod1.add("电水壶");
        prod1.add("手持电风扇");
        prod1.add("纸巾");
        prize1.setPrize(prod1);//设置奖品
        Prize<Integer> prize2 = new Prize<Integer>();
        ArrayList<Integer> prod2  = new ArrayList<Integer>();
        prod2.add(200);
        prod2.add(100);
        prod2.add(20);
        prod2.add(2);
        prize2.setPrize(prod2);
        System.out.println("奖品类型 1.实物 2.现金：");
        System.out.print("请选择奖品类型：");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if (n == 1)
            System.out.print("恭喜您抽到：" + prize1.getPrize());
        else if(n == 2)
            System.out.print("恭喜您抽到：" + prize2.getPrize() + "元");
        else
            System.out.print("输入错误");

    }
}
